package com.cxj.cxj_test_cloud.common.rxjava;

import com.cxj.cxj_test_cloud.common.network.response.ResponseData;
import com.cxj.cxj_test_cloud.utils.Logger;
import com.cxj.cxj_test_cloud.utils.ToastUtil;

public abstract class RxHttpResponseSubscriber<T extends ResponseData> extends MvvmResponseSubscriber<T>{
    private boolean mShowToast;

    public RxHttpResponseSubscriber() {
        this.mShowToast = true;//TODO 默认是显示Toast
    }
    @Override
    public void onNext(T response) {
        super.onNext(response);
        try {
            if (response.isRet()){
                onSuccess(response);
            }else {
                if (response.getCode() < 0 && mShowToast){
                    if (response.getCode()==ErrorCode.TOKEN_INVALID){
                        RxBus.getInstance().post(new TokenInvalidEvent());
                    }
                    ToastUtil.show(response.getMsg());
                    onError(response.getCode(), response.getMsg());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            Logger.e(e);
        }
    }
}
